package com.example.wordbook;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

public class Add_Word extends AppCompatActivity {
    Dbmanager dbmanager;
    Dbhelper dbhelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);
        dbhelper=new Dbhelper(this);
        dbmanager=new Dbmanager(dbhelper);
        Button button1=findViewById(R.id.add);
        Button button2=findViewById(R.id.change);
        Button button3=findViewById(R.id.delet);
        //Button button4=findViewById(R.id.check);
        Button button5=findViewById(R.id.updatesample);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1=findViewById(R.id.getegwordtosample);
                EditText editText2=findViewById(R.id.getsample);
                String egword=editText1.getText().toString().trim();
                String sample=editText2.getText().toString().trim();
                boolean i=dbmanager.insertsample(sample,egword);
                if(i){
                    TextView textView=findViewById(R.id.output);
                    textView.setText("插入例句成功");
                }
                else{
                    TextView textView=findViewById(R.id.output);
                    textView.setText("插入失败");
                }
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1=findViewById(R.id.getword);
                EditText editText2=findViewById(R.id.getchinese);
                EditText editText3=findViewById(R.id.get_sample);
                ContentValues contentValues=new ContentValues();
                contentValues.put("egword",editText1.getText().toString().trim());
                contentValues.put("cnword",editText2.getText().toString().trim());
                contentValues.put("sample",editText3.getText().toString().trim());
                boolean i=dbmanager.insertData("word",contentValues);
                TextView textView=findViewById(R.id.output);
                if(i){
                    textView.setText("插入成功");
                }
                else
                {
                    textView.setText("插入失败");
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1=findViewById(R.id.Cgetword);
                EditText editText2=findViewById(R.id.Cgetchinese);
                String word=editText1.getText().toString().trim();
                String chinese=editText2.getText().toString().trim();
                boolean i=dbmanager.changeData(word,chinese);
                TextView textView=findViewById(R.id.output);
                if(i){
                    textView.setText("更改成功");
                }
                else
                {
                    textView.setText("更改失败");
                }


            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText1=findViewById(R.id.Dgetword);
                String word=editText1.getText().toString().trim();
                boolean i=dbmanager.deleteData(word);
                TextView textView=findViewById(R.id.output);
                if(i){
                    textView.setText("删除成功");
                }
                else
                {
                    textView.setText("删除失败");
                }
            }
        });
        /*button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText=findViewById(R.id.checkword);
                String word=editText.getText().toString().trim();
                String ch=dbmanager.selectData(word);
                TextView textView=findViewById(R.id.output);
                if(!ch.isEmpty()){
                    textView.setText(ch);
                }
                else
                {

                }
            }
        });
        EditText get=findViewById(R.id.checkword);
        get.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                TextView textView1 = findViewById(R.id.egword1);
                TextView textView2 = findViewById(R.id.egword2);
                TextView textView3 = findViewById(R.id.egword3);
                TextView textView4 = findViewById(R.id.egword4);
                TextView textView5 = findViewById(R.id.egword5);
                textView1.setText("");
                textView2.setText("");
                textView3.setText("");
                textView4.setText("");
                textView5.setText("");


            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String egword=get.getText().toString().trim();
                ArrayList<String>firstword;
                if(!egword.isEmpty()) {
                    firstword = dbmanager.quey(egword);
                    TextView textView1 = findViewById(R.id.egword1);
                    TextView textView2 = findViewById(R.id.egword2);
                    TextView textView3 = findViewById(R.id.egword3);
                    TextView textView4 = findViewById(R.id.egword4);
                    TextView textView5 = findViewById(R.id.egword5);
                    int i = firstword.size();
                    if (i >= 5) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                        textView3.setText(firstword.get(2));
                        textView4.setText(firstword.get(3));
                        textView5.setText(firstword.get(4));
                    } else if (i >= 4) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                        textView3.setText(firstword.get(2));
                        textView4.setText(firstword.get(3));
                    } else if (i >= 3) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                        textView3.setText(firstword.get(2));
                    } else if (i >= 2) {
                        textView1.setText(firstword.get(0));
                        textView2.setText(firstword.get(1));
                    } else if (i >= 1) {
                        textView1.setText(firstword.get(0));
                    }
                    else {
                        textView1.setText("");
                        textView2.setText("");
                        textView3.setText("");
                        textView4.setText("");
                        textView5.setText("");

                    }
                }
            }
        });*/

    }


}